<?php

namespace Admin\Controller;

use Zend\Form\Annotation\AnnotationBuilder;
use Admin\Form\SearchForm;
use Admin\Controller\IndexController;
use Admin\Model\DBO\SearchReaderDBO;

class SearchReaderController extends IndexController {

    public $form;

    public function getForm() {
        if (!$this->form) {
            $cataloging = new SearchForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($cataloging);
            $this->form->get('keySearch')->setOptions(
                    array(
                        'value_options' => array(
                            '0' => 'Tìm trong',
                            '1' => 'Mã thẻ',
                            '2' => 'Họ tên',
                        ),
                    )
            );
        }

        return $this->form;
    }

    function Replace_TiengViet($str) {

        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/i", 'a', $str);

        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/i", 'e', $str);

        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/i", 'i', $str);

        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/i", 'o', $str);

        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/i", 'u', $str);

        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/i", 'y', $str);

        $str = preg_replace("/(đ)/i", 'd', $str);



        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);

        $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);

        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);

        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);

        $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);

        $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);

        $str = preg_replace("/(Đ)/", 'D', $str);

        //$str = str_replace(" ", "-", str_replace("&*#39;","",$str)); 
        // sử dụng cho ai có mục đích rewriteURL 

        return $str;
    }

    /*
     * Nếu không phải là quyền admin thì không thể vào mục này
     */

    public function indexAction() {
        // kiem tra neu chua dang nhap thi quay ve lai trang dang nhap
        // Nếu quyền bằng 2 thì ko duoc vao
        $this->checkRole2();
        // Neu dang nhap roi thi hien thi len sach sach nguoi dung
        // Lay thuc the dang nhap dua ra layout
        $this->layout()->identity = $this->getAuthService()->getIdentity();
        $form = $this->getForm();
        $request = $this->getRequest();
        if ($request->isPost()) {
            $myMessage = array();
            $result = null;
            $allAuthor = array();
            $dataRequest = $request->getPost();
            $form->setData($dataRequest);
            if ($form->isValid()) {
                $data = $form->getData();
                $keySearch = $data['keySearch'];
                $valueSearch = $data['valueSearch'];
                if ($keySearch == 1) {
                    // Tim kiem theo ma the ban doc
                    $filter = new \Zend\Filter\Word\SeparatorToSeparator('.', '-');
                    $valueSearch = $filter->filter($valueSearch);
                    $this->redirect()->toRoute('search-reader/default', array('action' => 'searchReaderBarcode', 'id' => $valueSearch));
                } else if ($keySearch == 2) {
                    // Tìm kiếm theo tên
                    $filter = new \Zend\I18n\Filter\Alpha(true);
                    $filter2 = new \Zend\Filter\Word\SeparatorToSeparator();
                    $valueSearch = $filter->filter($valueSearch);
                    $valueSearch = $filter2->filter($valueSearch);
                    $valueSearch = $this->Replace_TiengViet($valueSearch);
                    $this->redirect()->toRoute('search-reader/default', array('action' => 'searchReaderName', 'id' => $valueSearch));
                } else if ($keySearch == 3) {
                    
                } else if ($keySearch == 4) {
                    
                } else {
                    // ban chua nhap du thong tin tim kiem
                    $myMessage['inputError'] = 'inputError';
                }
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'form' => $form,
                'myMessage' => $myMessage,
            );
        }
        return array(
            'form' => $form,
        );
    }

    public function searchReaderNameAction() {
        // KT đăng nhập
        $this->checkRole2();
        $this->layout()->identity = $this->getAuthService()->getIdentity();
        $form = $this->getForm();
        $valueSearch = $this->params()->fromRoute('id', 0);
        $filter = new \Zend\Filter\Word\SeparatorToSeparator('-', ' ');
        $valueSearch = $filter->filter($valueSearch);
        $objectSearchReader = new SearchReaderDBO($this->getServiceLocator());
        $myMessage = array();
        $paginator = null;
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($objectSearchReader->searchReaderName($valueSearch));
        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
        $paginator->setItemCountPerPage(10);
        $paginator->setPageRange(5);
        if ($paginator == null) {
            $myMessage['notFoundResult'] = 'notFoundResult';
        }
        return array(
            'form' => $form,
            'myMessage' => $myMessage,
            'paginator' => $paginator,
        );
    }

    public function searchReaderBarcodeAction() {
        // KT đăng nhập
        $this->checkRole2();
        $form = $this->getForm();
        $valueSearch = $this->params()->fromRoute('id', 0);
        $filter = new \Zend\Filter\Word\SeparatorToSeparator('-', '.');
        $valueSearch = $filter->filter($valueSearch);
        $objectSearchReader = new SearchReaderDBO($this->getServiceLocator());
        $myMessage = array();
        $paginator = null;
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($objectSearchReader->searchReaderBarcode($valueSearch));
        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
        $paginator->setItemCountPerPage(10);
        $paginator->setPageRange(5);
        if ($paginator == null) {
            $myMessage['notFoundResult'] = 'notFoundResult';
        }
        return array(
            'form' => $form,
            'myMessage' => $myMessage,
            'paginator' => $paginator,
        );
    }

// Ket thuc lop
}